package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.*;
import com.qf.service.*;
import com.qf.util.ResponseUtil;
import com.qf.vo.CatVo;
import com.qf.vo.GoodsAllinone;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wanghaotian
 * time:2022/12/21,0021 8:57
 * description:
 */

@RestController
@CrossOrigin("*")
@RequestMapping("/admin/goods")
public class GoodsController {

    @Autowired
    private DtsGoodsService goodsService;

    @Autowired
    private DtsCategoryService categoryService;

    @Autowired
    private DtsGoodsAttributeService attributeService;

    @Autowired
    private DtsGoodsSpecificationService goodsSpecificationService;

    @Autowired
    private DtsGoodsProductService goodsProductService;

    @Autowired
    private DtsBrandService brandService;

    @GetMapping("/list")
    public Object list(
            @RequestParam(value = "page",required = false,defaultValue = "1") Integer page,
            @RequestParam(value = "limit",required = false,defaultValue = "20") Integer limit,
            String goodsSn,String name,String sort,String order
    ){
        IPage<DtsGoods> iPage = goodsService.findPage(page,limit,goodsSn,name,sort,order);
        Map<String,Object> data = new HashMap<>();
        data.put("total", iPage.getTotal());
        data.put("items", iPage.getRecords());
        return ResponseUtil.ok(data);
    }

    @PostMapping("/create")
    public Object create(@RequestBody GoodsAllinone goodsAllinone){
        goodsService.add(goodsAllinone);
        return ResponseUtil.ok();
    }

    @GetMapping("/detail")
    public Object detail(Integer id){
        DtsGoods goods = goodsService.selectById(id);
        Integer categoryIdL2 = goods.getCategoryId();
        Integer categoryIdL1 = categoryService.findL1ByL2(categoryIdL2);
        Integer[] categoryIds = {categoryIdL1,categoryIdL2};
        List<DtsGoodsAttribute> attributes = attributeService.findAttributesByGoodsId(id);
        List<DtsGoodsSpecification> specifications = goodsSpecificationService.findSpecificationByGoodsId(id);
        List<DtsGoodsProduct> products = goodsProductService.findProductByGoodsId(id);
        Map<String,Object> data = new HashMap<>();
        data.put("categoryIds",categoryIds);
        data.put("goods",goods);
        data.put("attributes",attributes);
        data.put("specifications",specifications);
        data.put("products",products);
        return ResponseUtil.ok(data);
    }

    @GetMapping("/catAndBrand")
    public Object catAndBrand(){
        Subject subject = SecurityUtils.getSubject();
        DtsAdmin admin = (DtsAdmin) subject.getPrincipal();
        List<Integer> brandIds = null;
        if (subject.hasRole("品牌制造商")) {
            brandIds = brandService.findBrandIds(admin.getId());
        }
        List<Map<String,Object>> brandList = brandService.findBrandList(brandIds);
        List<CatVo> catVos = categoryService.findCategoryList();
        Map<String,Object> data = new HashMap<>();
        data.put("categoryList",catVos);
        data.put("brandList",brandList);
        return ResponseUtil.ok(data);
    }

    @PostMapping("/update")
    public Object update(@RequestBody GoodsAllinone goodsAllinone){
        try {
            goodsService.update(goodsAllinone);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return ResponseUtil.fail(-1, e.getMessage());
        }
        return ResponseUtil.ok();
    }

    @PostMapping("/delete")
    public Object delete(@RequestBody DtsGoods goods){
        try {
            goodsService.delete(goods);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return ResponseUtil.fail(-1, e.getMessage());
        }
        return ResponseUtil.ok();
    }
}
